package com.zhida.smartcontrol.dialog.devicelistdialog;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import com.jay.tools.ui.ButtonStyle;
import com.tan.linkpicker.pickerview.builder.OptionsPickerBuilder;
import com.tan.linkpicker.pickerview.listener.CustomListener;
import com.tan.linkpicker.pickerview.listener.OnOptionsSelectListener;
import com.tan.linkpicker.pickerview.view.OptionsPickerView;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;
import com.zhida.smartcontrol.pagectrl.ProjectListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * author: tanjianpei
 * date: 2019/12/16 016 16:51
 * description: 设备列表
 */
public class DeviceListDialogFragment extends MVPBaseDialogFragment<DeviceListDialogContract.View, DeviceListDialogPresenter> implements DeviceListDialogContract.View {
    @BindView(R.id.fl_container)
    FrameLayout mFrameLayout;

    private ProjectListBean mBean;

    private OptionsPickerView pvCustomOptions;
    private List<ProjectListBean> mProjectList;

    public static DeviceListDialogFragment newInstance() {
        Bundle args = new Bundle();
        DeviceListDialogFragment fragment = new DeviceListDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        mShowBottom = true;
        return R.layout.dialog_device_list;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        initCustomOptionPicker();
        mPresenter.requestProjectList();
    }

    @Override
    public void showList(List<ProjectListBean> projectList) {
        mProjectList = new ArrayList<>(projectList);
        mBean = projectList.get(0);

        pvCustomOptions.setPicker(projectList);//添加数据
        pvCustomOptions.show();
    }

    public interface OnSelListener {
        void onSel(Object object, boolean isChange);
    }

    /**
     * 条件选择器初始化，自定义布局
     */
    private void initCustomOptionPicker() {
        pvCustomOptions = new OptionsPickerBuilder(mActivity, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                mBean = mProjectList.get(options1);
            }
        })
                .setLayoutRes(R.layout.dialog_device_picker, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final ButtonStyle btnCancel = v.findViewById(R.id.btn_cancel);
                        final ButtonStyle btnConfirm = v.findViewById(R.id.btn_confirm);
                        btnConfirm.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.returnData();
                                pvCustomOptions.dismiss();

                                if (null != mListener) {
                                    mListener.onDestroy(mBean, true);
                                }
                                dismissAllowingStateLoss();
                            }
                        });

                        btnCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.dismiss();
                                dismissAllowingStateLoss();
                            }
                        });

                    }
                })
                .isDialog(false)
                .setItemVisible(17)
                .setLineSpacingMultiplier(2.0f)
                .setDecorView(mFrameLayout)
                .setOutSideCancelable(false)
                .setOutSideColor(Color.TRANSPARENT)
                .build();
    }

}
